/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.knowledge.plans;

import com.knowledge.facts.Condition;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author keithballantyne
 */
public class PlanFactory {
    private ArrayList<Class> plans;
    private static PlanFactory instance=null;
    
    protected PlanFactory() {
        plans = new ArrayList<Class>();
        
        plans.add(ExplorePlan.class);
        plans.add(GotoFoodPlan.class);
    }
    
    public static PlanFactory getInstance() {
        if (instance == null) instance = new PlanFactory();
        return instance;
    }
    
    public List<Plan> findPlans(Condition postCondition) {
        Set<Plan> candidates = new HashSet<Plan>();
        
        for (Class cl: plans) {
            try {
                Plan trial = (Plan) cl.newInstance();
                
                for (Condition cond: trial.getPostconditions()) {
                    if (postCondition.equals(cond)) candidates.add(trial);
                }
                
            } catch (InstantiationException ex) {
                Logger.getLogger(PlanFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(PlanFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return new ArrayList<Plan>(candidates);
    }
    
}
